<?php

class Marketing extends CI_Controller {

	function __construct()
	{
		parent:: __construct();
		$this->load->helper('bantuan_helper');
		$this->load->model('Marketing_model');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		elseif($this->session->userdata('user_rule')=='Sales'):
			redirect('backend/no_acces');
		endif;

	}

	function index()
	{
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/marketing/index';
		$config['total_rows'] = count($this->Marketing_model->getMarketing('all',FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
			echo $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Marketing';
		$data['template'] = 'marketing/index';
		$data['res'] = $this->Marketing_model->getMarketing('all',FALSE,$config['per_page'],$offset);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}

	function add()
	{
		$this->form_validation->set_rules('nm_marketing', 'Nama Marketing','required');
		$this->form_validation->set_rules('code_marketing', 'Code Marketing','required');
		$this->form_validation->set_rules('address', 'Alamat Marketing','required');
		$this->form_validation->set_rules('id_province', 'Provinsi Marketing','required');
		$this->form_validation->set_rules('id_kab', 'Kabupaten Marketing','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['title'] = 'Marketing';
			$data['template'] = 'marketing/add';
			$this->load->view('backend/index',$data);
		}
		else
		{
			$data = array
				(
				'nm_marketing'=>$this->input->post('nm_marketing'),
				'code_marketing'=>$this->input->post('code_marketing'),
				'address'=>$this->input->post('address'),
				'id_province'=>$this->input->post('id_province'),
				'id_kab'=>$this->input->post('id_kab')
				);
			$this->Marketing_model->addMarketing($data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Satu data Berhasil ditambahkan</div>');
			redirect('backend/marketing');
		}	
	}

	function edit()
	{
		$this->form_validation->set_rules('nm_marketing', 'Nama Marketing','required');
		$this->form_validation->set_rules('code_marketing', 'Code Marketing','required');
		$this->form_validation->set_rules('address', 'Alamat Marketing','required');
		$this->form_validation->set_rules('id_province', 'Provinsi Marketing','required');
		$this->form_validation->set_rules('id_kab', 'Kabupaten Marketing','required');

		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['title'] = 'Marketing';
			$data['template'] = 'marketing/edit';
			$data['row'] = $this->Marketing_model->getMarketing(FALSE,$id,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else
		{
			$id = $this->uri->segment(4);
			$data = array
				(
				'nm_marketing'=>$this->input->post('nm_marketing'),
				'code_marketing'=>$this->input->post('code_marketing'),
				'address'=>$this->input->post('address'),
				'id_province'=>$this->input->post('id_province'),
				'id_kab'=>$this->input->post('id_kab')
				);
			$this->Marketing_model->editMarketing($id,$data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil diupdate</div>');
			redirect('backend/marketing/edit/'.$id);
		}
			
	}

	function delete($id)
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$this->Marketing_model->deleteMarketing($id);
		$count = count($this->Marketing_model->getMarketing('all',FALSE,FALSE,FALSE));
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil dihapus</div>');
		getPage('marketing','delete',$uri,$count);
	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["marketing_id"] as $id):
			$this->db->where('id_marketing',$id);
			$this->db->delete('tk_marketing');
		endforeach;
	}
	
	function ajax_showKab(){
		$provId=$this->uri->segment(4);
		$this->db->where('id_province',$provId);
		$data['kab']= $this->db->get('tk_kab')->result_array();
		$this->load->view('backend/marketing/help_kab',$data);
	}

}